package view.renderers;

import java.awt.Shape;
import java.awt.geom.GeneralPath;
import java.util.List;

import prefuse.render.AbstractShapeRenderer;
import prefuse.visual.VisualItem;
import prefuse.visual.VisualTable;
import view.visualizations.XCVis;

/**
 * Because prefuse doesn't support line graphs?
 */
public class LineRenderer extends AbstractShapeRenderer {

	private VisualTable dataTable;
	
	private GeneralPath m_line;
	private List<String> selectedNames;
	
	public LineRenderer(final XCVis vis) {
		this.dataTable = (VisualTable) vis.getGroup("data");
		m_line = new GeneralPath();
		selectedNames = vis.getSelectedNames();
	}
	
	@Override
	protected Shape getRawShape(final VisualItem curItem) {
		m_line.reset();
		
		
		int nextRow = curItem.getRow()+1;
		double x1 = curItem.getX();
		double y1 = curItem.getY();
		int sSize = 3;
		
		if (selectedNames.contains(curItem.get("Name"))) {
			for (int i=-sSize; i<=sSize; i++) {
				m_line.moveTo(x1-sSize, y1+i);
				m_line.lineTo(x1+sSize, y1+i);
			}
		} else {
			m_line.moveTo(x1-sSize, y1-sSize);
			m_line.lineTo(x1+sSize, y1-sSize);
			m_line.lineTo(x1+sSize, y1+sSize);
			m_line.lineTo(x1-sSize, y1+sSize);
			m_line.lineTo(x1-sSize, y1-sSize);
		}
		
		if ((nextRow >= 0) && (nextRow < dataTable.getRowCount())) {
			Object nextName = dataTable.get(nextRow, "Name");
			Object curName = curItem.get("Name");
			Object nextSchool = dataTable.get(nextRow, "School");
			Object curSchool = curItem.get("School");
			if (curName.equals(nextName) && curSchool.equals(nextSchool)) {
				VisualItem nextItem = dataTable.getItem(curItem.getRow()+1);
				
				double x2 = nextItem.getX();
				double y2 = nextItem.getY();
				
				m_line.moveTo(x1, y1);
				m_line.lineTo(x2, y2);
				if (selectedNames.contains(curItem.get("Name"))) {
					m_line.moveTo(x1, y1-1);
					m_line.lineTo(x2, y2-1);
					m_line.moveTo(x1, y1+1);
					m_line.lineTo(x2, y2+1);
				}
			}
		}
		return m_line;
	} 
}
